<template>
    <div>
        <!-- 面包屑 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 主题内容 -->
        <el-card>
            <el-row>
                <el-col>
                    <el-input 
                    placeholder="请输入内容" 
                    v-model="queryInfo.query" 
                    class="input-with-select"
                    clearable
                    @clear="getGoodLists">
                        <el-button slot="append" icon="el-icon-search" @click="getGoodLists"></el-button>
                    </el-input>
                    <el-button type="primary" @click="goAddPage">添加商品</el-button>
                </el-col>
            </el-row>
            <el-table
                :data="goodListTableData"
                border
                style="width: 100%">
                <!-- 索引列 -->
                <el-table-column 
                label="#"
                type="index"
                align="center">
                </el-table-column>
                <el-table-column
                    prop="goods_name"
                    label="商品名称">
                </el-table-column>
                <el-table-column
                    prop="goods_price"
                    label="商品价格(元)"
                    width="100"
                    align="center">
                </el-table-column>
                <el-table-column
                    prop="goods_weight"
                    label="商品重量"
                    width="70"
                    align="center">
                </el-table-column>
                <el-table-column
                    label="创建时间"
                    width="150"
                    align="center">
                    <!-- 自定义时间，使用过滤器 -->
                    <template slot-scope="scope">
                        {{ scope.row.add_time | dateFormat }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    width="150"
                    align="center">
                    <template slot-scope="scope">
                        <!-- 编辑按钮 -->
                        <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
                        <!-- 删除按钮 -->
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeGoodsListById(scope.row.goods_id)"></el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页功能 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
    export default {
        name: 'List',
        data() {
            return {
                inputGoodList: '', // 用户输入的搜索框
                goodListTableData:[], // 表格的数据
                // 获取商品列表的参数对象
                queryInfo:{
                    query : '', // 查询的参数
                    pagenum : 1, // 当前的页码  不能是字符串
                    pagesize : 10 // 每页显示条数
                },
                total:0,  //总的数据条数(不能写 total='' , 要写 total:0 ，否则报错说这是字符串不是数字)
            }
        },
        created(){  // 生命周期钩子函数
            this.getGoodLists() // 获取商品列表的数据
        },
        methods:{
            // 获取商品列表的数据
            async getGoodLists(){
                const {data : res} = await this.$http.get('goods',{params:this.queryInfo})
                if(res.meta.status != 200) return this.$message.error(res.meta.msg)
                // this.$message.success(res.meta.msg)
                this.goodListTableData = res.data.goods
                this.total = res.data.total
            },
            // pageSize 改变时会触发（选择一页显示多少条）
            handleSizeChange(newSize){
                // console.log(newSize);
                this.queryInfo.pagesize = newSize
                this.getGoodLists() // 重新获取商品列表数据
            },
            // currentPage 改变时会触发（下面1，2，3...的改变）
            handleCurrentChange(newPage){
                // console.log(newPage);
                this.queryInfo.pagenum = newPage
                this.getGoodLists() // 重新获取商品列表数据
            },
            // 根据id删除商品列表
            async removeGoodsListById(goods_id){
                // console.log(goods_id);
                const confirmResult = await this.$confirm('是否将此商品列表永久删除？','提示',{
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).catch( err => err)
                if(confirmResult != 'confirm'){
                    return this.$message.info('取消删除')
                }
                const {data: res} = await this.$http.delete(`goods/${goods_id}`)
                if(res.meta.status != 200) return this.$message.error(res.meta.msg)
                this.$message.success(res.meta.msg)
                this.getGoodLists()  // 重新获取商品列表
                // console.log(res);
            },
            // 添加商品按钮的事件
            goAddPage(){
                this.$router.push('/goods/add')  // 编程式导航
            }
        }
    }
</script>

<style scoped>
.input-with-select{
    width: 300px;
    margin-right: 20px;

}
</style>